package org.example.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.SysUser;
import org.example.mapper.SysUserMapper;
import org.example.mone.mapper.UserMapper;
import org.example.mone.model.User;
import org.example.serivce.ISysUserService;
import org.postgresql.copy.CopyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserService implements ISysUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserService.class);

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private UserMapper userMapper;

    public List<SysUser> selectAll() {
        return sysUserMapper.selectAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUser(SysUser user) {
        sysUserMapper.insert(user);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public PageInfo<SysUser> selectByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserMapper.selectByPage(pageSize, pageSize);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    @Override
    public IPage<User> selectByIPage(int pageNum, int pageSize) {
        Page<User> page = new Page(pageNum,pageSize);
        page.addOrder(OrderItem.asc("id"));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        List<User> userPage = userIPage.getRecords();
//        LOGGER.info("size -------------> {}",userPage.size());
//        LOGGER.info("总条数 -------------> {}",userIPage.getTotal());
//        LOGGER.info("当前页数 -------------> {}",userIPage.getCurrent());
//        LOGGER.info("当前每页显示数 -------------> {}",userIPage.getSize());
        return userIPage;
    }

    @Override
    public PageInfo<User> selectByPageHelper(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectByPageHelper();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }




}
